package com.hxzy.controller.admin.dashboard.strategy;

import cn.hutool.core.date.DateUtil;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.controller.admin.dashboard.resp.ReportResp;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/17 14:49
 * @Version 1.0
 */
@Log4j2
@Component
public class RedisCurrentYearReport implements ContamerReportStrategy{

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public ReportResp calcReport() {
        //统计  1-12   现在7月
        int year=DateUtil.year(new Date());
        int month=DateUtil.month(new Date())+1;

        List<String> xAxis=new ArrayList<>();
        List<Object> series=new ArrayList<>();

        for(int i=1;i<=month;i++){
            xAxis.add(i+"月");

            //统计redis中的值
            String ym=year+""+((i<10)?"0"+i: i);
            String redisKey= RedisConstans.getRedisKey(RedisConstans.CONTAMER_HASH_KEY,ym);
            List<Integer> values = this.redisTemplate.opsForHash().values(redisKey);
            //该没有数据
            if(values.size()==0){
                series.add(0);
            }else{
                //每天累进行累加
                Integer sum=0;
                for(Integer  v : values){
                    sum+= v;
                }
                series.add(sum);
            }
        }

        ReportResp  reportResp=new ReportResp();
        reportResp.setXAxis(xAxis);
        reportResp.setSeries(series);
        return reportResp;
    }
}
